package com.zzsy.carosshopprojects.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.view.CusWebView;

import androidx.annotation.NonNull;


/**
 * 带有一个按钮的dialog
 * Created by Administrator on 2018/3/21 0021.
 */
public class ComOneBtnDialog extends Dialog implements View.OnClickListener {


    public static final int DIALOG_PAY_FAIL = 1;
    public static final int DIALOG_VERSION_UPDATE = 2;
    public static final int DIALOG_DISABLE = 3;
    public static final int DIALOG_VERSIONS = 4;
    public static final int DIALOG_NOT_UPDATE = 5;
    public static final int DIALOG_NOT_WIFI = 6;
    public static final int DIALOG_NO_PAY = 7;
    public static final int DIALOG_FAIL_PAY = 8;
    public static final int DIALOG_NO_DATA_CUO = 9;
    private Activity activity;
    private int dialogType;
    private int type = 1;
    private String tipStr;
    private String tipTitleStr;
    private String btnName;
    private String updateContents;
    private IDialogBtnClickListener dialogBtnClickListener;
    private CusWebView webView;
    private TextView tipTititle;

    public interface IDialogBtnClickListener {
        void onDialogBtnClick();
        void onDialogGoMarkerBtnClick();
    }

    public ComOneBtnDialog(@NonNull Activity context, int dialogType, String updateContent) {
        this(context, R.style.BottomPopupDialog, dialogType, updateContent);
    }

    public ComOneBtnDialog(@NonNull Activity context, int themeResId, int dialogType, String updateContent) {
        super(context, themeResId);

        this.activity = context;
        this.dialogType = dialogType;
        this.updateContents = updateContent;
        switch (dialogType) {
            case DIALOG_PAY_FAIL:
                tipStr = "温馨提示";
                tipTitleStr = "该订单正在支付中哦...";
                btnName = "取消订单";
                break;

            case DIALOG_VERSION_UPDATE:
                tipStr = "温馨提示";
                type = 2;
                updateContents = updateContent;
                btnName = "确定";
                break;
            case DIALOG_DISABLE:
                tipStr = "温馨提示";
                tipTitleStr = "当前账号已被禁用！";
                btnName = "确定";
                break;

            case DIALOG_VERSIONS:
                tipStr = "温馨提示";
                tipTitleStr = "新版本已安排,请到应用市场更新 ！";
                btnName = "退出";
                break;

            case DIALOG_NOT_UPDATE:
                tipStr = "温馨提示";
                tipTitleStr = "新版本来了，请尽快到应用市场更新！";
                btnName = "知道了";
                break;
            case DIALOG_NOT_WIFI:
                tipStr = "温馨提示";
                tipTitleStr = "当前网络不可用，请检查网络！";
                btnName = "确定";
                break;
            case DIALOG_NO_PAY:
                tipStr = "温馨提示";
                tipTitleStr = "暂未收到支付结果，请联系工作人员！";
                btnName = "确定";
                break;
            case DIALOG_FAIL_PAY:
                tipStr = "温馨提示";
                tipTitleStr = "支付失败，请联系工作人员！";
                btnName = "确定";
                break;
            case DIALOG_NO_DATA_CUO:
                tipStr = "温馨提示";
                tipTitleStr = "数据异常，请联系工作人员！";
                btnName = "确定";
                break;
            // 设置更多样式
            default:
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        setContentView(R.layout.dialog_com_one_btn);
        ScreenAdapterTools.getInstance().loadView(getWindow().getDecorView());
        initUI();
    }

    private void initUI() {
        TextView tipText = findViewById(R.id.tip_tv);
        tipTititle = findViewById(R.id.tip_title);
        TextView sureBtn = findViewById(R.id.sure_btn);
        sureBtn.setText(btnName);
        webView = findViewById(R.id.cusview);
        sureBtn.setOnClickListener(this);

        TextView go_marker = findViewById(R.id.go_marker);
        go_marker.setOnClickListener(this);

        if (type == 2) {
            webView.setVisibility(View.VISIBLE);
            tipTititle.setVisibility(View.GONE);
            webView.loadHtmlString(updateContents);
        } else {
            webView.setVisibility(View.GONE);
            tipTititle.setVisibility(View.VISIBLE);
            tipTititle.setText(tipTitleStr);
        }
        if (dialogType == ComOneBtnDialog.DIALOG_VERSIONS) {
            go_marker.setVisibility(View.VISIBLE);
        } else {
            go_marker.setVisibility(View.GONE);
        }

        tipText.setText(tipStr);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sure_btn:
                dialogBtnClickListener.onDialogBtnClick();
                dismiss();
                break;
            case R.id.go_marker://去应用市场更新
                dialogBtnClickListener.onDialogGoMarkerBtnClick();
                break;
            default:
                break;
        }
    }

    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
//        layoutParams.alpha = 0.5f;
//        layoutParams.dimAmount = 0.5f;
        activity.getWindow().setAttributes(layoutParams);
    }

    @Override
    public void dismiss() {
        super.dismiss();

        WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
        layoutParams.alpha = 1.0f;
        layoutParams.dimAmount = 1.0f;
        activity.getWindow().setAttributes(layoutParams);
    }

    public void setDialogBtnClickListener(IDialogBtnClickListener dialogBtnClickListener) {
        this.dialogBtnClickListener = dialogBtnClickListener;
    }

}
